package ro.licenta.room.bedroom;

import ro.licenta.utilities.utils.StaticUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.example.test.R;

public class BedroomDialog extends Activity implements OnTouchListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setTheme(android.R.style.Theme_Dialog);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog);
		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new BedroomAdapter(this));

		// gridview.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View v,
		// int position, long id) {
		// Intent returnIntent = new Intent();
		// returnIntent.putExtra(StaticUtils.roomId,
		// StaticUtils.roomBedroom);
		// returnIntent.putExtra(StaticUtils.positionId, position);
		// setResult(RESULT_OK, returnIntent);
		// finish();
		// }
		// });
		gridview.setOnItemClickListener(myItemClickListener);

		Window window = this.getWindow();
		window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
				WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
				WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
	}

	private OnItemClickListener myItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			Intent returnIntent = new Intent();
			returnIntent.putExtra(StaticUtils.roomId, StaticUtils.roomBedroom);
			returnIntent.putExtra(StaticUtils.positionId, position);
			setResult(RESULT_OK, returnIntent);
			finish();
		}
	};

	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
			this.finish();
		}
		return false;
	}
}
